package Thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-08-11
 * Time: 12:40
 */
public class demo40 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(4);//创建线程池

        CountDownLatch count = new CountDownLatch(20);//拆分成20个任务

        for (int i = 0; i < 20; i++) {
            int id = i;
            service.submit(() -> {
                System.out.println("下载 " + id + " 任务");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("下载 " + id + " 任务完毕");
                count.countDown();
            });
        }
        //当所count收到20个完成,此时所有的任务都完成了
        count.await();//相当于all wait
        System.out.println("所有任务都完成");
    }
}
